package com.meals.system.service.impl;

import com.meals.system.common.ServiceResultEnum;
import com.meals.system.dao.AdminUserMapper;
import com.meals.system.dao.WhitesMealAdminUserTokenMapper;
import com.meals.system.entity.AdminUser;
import com.meals.system.entity.AdminUserToken;
import com.meals.system.service.AdminUserService;
import com.meals.system.util.NumberUtil;
import com.meals.system.util.SystemUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private WhitesMealAdminUserTokenMapper whitesMealAdminUserTokenMapper;

    @Override
    public String login(String userName, String password){
        AdminUser loginAdminUser =adminUserMapper.login(userName,password);
        if (loginAdminUser != null){
            // 登录后即执行修改token的操作
            String token = getNewToken(System.currentTimeMillis() + "", loginAdminUser.getAdminUserId());
            AdminUserToken adminUserToken = whitesMealAdminUserTokenMapper.selectByPrimaryKey(loginAdminUser.getAdminUserId());
            // 当前时间
            Date now = new Date();
            // 过期时间
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000); // 过期时间48小时
            // 如果第一次登录 没有对应的adminUserToken这个对象,为token数据库添加新的一条token数据
            if (adminUserToken == null){
                adminUserToken = new AdminUserToken();
                adminUserToken.setAdminUserId(loginAdminUser.getAdminUserId());
                adminUserToken.setToken(token);
                adminUserToken.setUpdateTime(now);
                adminUserToken.setExpireTime(expireTime);
                // 新增一条token数据
                if (whitesMealAdminUserTokenMapper.insertSelective(adminUserToken) > 0){
                    // 新增成功后返回
                    return token;
                }
            } else {
                adminUserToken.setToken(token);
                adminUserToken.setUpdateTime(now);
                adminUserToken.setExpireTime(expireTime);
                // 更新
                if (whitesMealAdminUserTokenMapper.updateByPrimaryKeySelective(adminUserToken) > 0){
                    // 更新成功后返回
                    return token;
                }
            }
        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }


    /**
     * 获取token值
     * @param timeStr
     * @param userId
     * @return
     */
    private String getNewToken(String timeStr, Long userId){
        String src = timeStr + userId + NumberUtil.genRandomNum(6);
        return SystemUtil.genToken(src);
    }


    @Override
    public AdminUser getUserDetailById(Long loginUserId){
        return adminUserMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public Boolean updatePassword(Long loginUserId, String originalPassword, String newPassword){
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        // 当前用户非空才可以进行更改
        if (adminUser != null){
            // 比较原密码是否正确
            if (originalPassword.equals(adminUser.getLoginPassword())){
                // 设置新密码并修改
                adminUser.setLoginPassword(newPassword);
                if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0 && whitesMealAdminUserTokenMapper.deleteByPrimaryKey(loginUserId) > 0){
                    // 修改成功且清空当前token则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Long loginUserId, String loginUserName, String nickName){
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        // 当前用户非空才可以进行修改
        if (adminUser != null){
            // 设置新名称
            adminUser.setLoginUserName(loginUserName);
            adminUser.setNickName(nickName);
            if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0){
                //修改成功返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean logout(Long adminUserId){
        return whitesMealAdminUserTokenMapper.deleteByPrimaryKey(adminUserId) > 0;
    }





}
